package cn.com.nou.base.controller.bdm;

import cn.com.nou.base.dao.bdm.LevelMapper;
import cn.com.nou.base.service.bdm.CourseCategoryService;
import cn.com.nou.base.service.bdm.LevelService;
import cn.com.nou.common.BaseResponse;
import cn.com.nou.common.entity.NouEnumerationT;
import cn.com.nou.common.entity.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("level")
public class LevelController {
    @Autowired
    LevelService levelService;
    @Autowired
    LevelMapper levelMapper;

    @GetMapping("list")
    public ResponseEntity getLevelList(@RequestBody NouEnumerationT nouEnumerationT, int pageNo, int pageSize) {
        return BaseResponse.generateOKResponseEntity(levelService.getLevelList(nouEnumerationT, pageNo, pageSize));
    }

    @PostMapping("add")
    public ResponseEntity addLevel(@RequestBody NouEnumerationT nouEnumerationT) {
        int code = levelService.addLevel(nouEnumerationT);
        if (code == 1) {
            return BaseResponse.generateOKResponseEntity("添加成功");
        } else if (code == 2) {
            return BaseResponse.generateBadResponseEntity(500, "error", "itemCode或者ItemName冲突");
        } else {
            return BaseResponse.generateBadResponseEntity(500, "error", "添加失败");
        }
    }

    @PostMapping("delete")
    public ResponseEntity deleteLevel(@RequestBody NouEnumerationT nouEnumerationT) {
        List<Integer> codes = levelService.deleteLevel(nouEnumerationT);
        StringBuffer badMessage = new StringBuffer();
        if (codes.contains(4)) {
            return BaseResponse.generateOKResponseEntity("删除成功");
        }
        if (codes.contains(1)) {
            badMessage.append("层次被学生使用,");
        }

        if (codes.contains(2)) {
            badMessage.append("层次被院校使用,");
        }

        if (codes.contains(3)) {
            badMessage.append("层次被使用");
        }
        return BaseResponse.generateBadResponseEntity(500, "error", badMessage.toString());
    }

    @PostMapping("update")
    public ResponseEntity updateLevel(@RequestBody NouEnumerationT nouEnumerationT) {
        int code = levelService.updateLevel(nouEnumerationT);
        if (code > 0) {
            return BaseResponse.generateOKResponseEntity("更新成功");
        }
        return BaseResponse.generateBadResponseEntity(500,"error","更新失败");
    }

    //    public int updateLevel(@RequestBody NouEnumerationT nouEnumerationT){
//    }
    @GetMapping("test")
    public int test() {
//        System.out.println(courseCategoryMapper.getCourseCategoryListCount("courseCategory"));
        NouEnumerationT nouEnumerationT = new NouEnumerationT();
        nouEnumerationT.setType("courseCategory");
        nouEnumerationT.setId("31D8FB675B614850BE1C2BF5F3EEDC70");
        nouEnumerationT.setItemCode("00000");
        nouEnumerationT.setItemName("问答");
//        return courseCategoryMapper.getUsedEnumCount(nouEnumerationT);
//        System.out.println(courseCategoryMapper.getSameItemCodeOrItemName(nouEnumerationT).size()==0);
//        return levelMapper.getLevelCount("educationLevel");
        return levelMapper.getStudentUsedCount(nouEnumerationT);
    }
}
